/**
 * Created by user on 2015/9/7.
 */

var questionStoreService = require('../../../services/mis/master/questionStoreService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');

exports.questionStoreList = function(req, res) {

    questionStoreService.questionStoreListService(req, function (err, results) {
        logger.debug('questionStoreLis');
        if (err) {
            logger.error('Exception happen when questionStoreList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when questionStoreList...'));
        } else {
            var data={};
            var data = {
                count: results[0]["count"],
                totalPage: results[0]["page"],
                list: results[1]
            };
            //logger.debug('data: ');
            //logger.debug(data);
            res.json(messageUtil.composeSucceededMessage(req, null, data));

        }

    });
};


exports.questionStoreAdd = function(req, res) {

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('postQuestionNew - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
        } else {
            var tempImages = new Array();
            var k = 0;
            for (var i = 0; i < images.length; i++) {
                if (images[i] != null)
                    tempImages[k++] = {
                        "imageURL": images[i]
                    }
            }
            req.body.images = tempImages;

            questionStoreService.questionStoreAddService(req, function (err, result) {
                if (err) {
                    res.json(messageUtil.composeFailedMessage(req, 'Exception happen when questionStore add...'));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    });

};

exports.questionStoreDelete = function(req, res) {

    var ids = req.body.stroeId;
    logger.debug(ids);
    questionStoreService.questionStoreDeleteService(ids, function(err, results) {

        if (err) {
            logger.error('Exception happened when trying to delete questionnStore...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when questionStoreDelete...'));
        }else {
            if (results != null) {
                logger.debug("Delete one question  successfully...!");
                res.json(messageUtil.composeSucceededMessage(req, null, null));
            } else {
                logger.error('Failed to delete questionnStore, it is not existing! -' + id);
                res.json(messageUtil.composeFailedMessage(req, 'Failed to delete questionnStore , it is not existing! -' + id));
            }
        }

    });
};

exports.questionStoreEdit = function(req, res) {

    questionStoreService.questionStoreEditService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when questionStoreEdit...'));
        }else {

            if (err) {
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when questionStoreEdit...'));
            } else {
                res.json(messageUtil.composeSucceededMessage(req, null, results));
            }
        }
    });
};


exports.questionPost = function(req, res) {

    questionStoreService.questionPostService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when questionStoreEdit...'));
        }else {

            if (err) {
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when questionStoreEdit...'));
            } else {
                res.json(messageUtil.composeSucceededMessage(req, null, {}));
            }
        }
    });
};





